using System;
using System.Collections.Generic;
using System.Text;

using NSimpleDB.Service.Contract;
using Forum.Contracts;

namespace Forum.DataAccess.Mapping
{
    static class Mapper
    {
        internal const string ForumsDomainName = "Forums";

        internal static ISimpleDBAttribute[] MapPosting2Attributes(Posting posting)
        {
            List<ISimpleDBAttribute> attributes = new List<ISimpleDBAttribute>();

            attributes.Add(new SimpleDBAttribute("subject", posting.Subject));
            //TODO: split text across multiple attribute values
            attributes.Add(new SimpleDBAttribute("text", posting.Text));
            attributes.Add(new SimpleDBAttribute("createdAt", posting.CreatedAt.ToString("s")));
            attributes.Add(new SimpleDBAttribute("createdBy", posting.CreatedBy));

            if (posting.Parent == null)
                attributes.Add(new SimpleDBAttribute("isRoot", "true"));

            return attributes.ToArray();
        }


        internal static Posting MapAttributes2Posting(IDataAccess caller, ISimpleDBAttribute[] attributes, string postingId, Posting parent)
        {
            Posting p = new Posting(caller, postingId, parent);
            foreach (ISimpleDBAttribute a in attributes)
                switch (a.Name)
                {
                    case "subject":
                        p.Subject = a.Value;
                        break;
                    case "text":
                        p.Text = a.Value;
                        break;
                    case "createdAt":
                        p.CreatedAt = DateTime.Parse(a.Value);
                        break;
                    case "createdBy":
                        p.CreatedBy = a.Value;
                        break;
                }
            return p;
        }
    }
}
